package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.dto.CommodityDto;
import com.kust.smarthome.web.model.Collection;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.ICollectionService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.CollectionVo;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/collection")
@Api(value = "收藏相关", tags = {"shopping-tag"})
public class CollectionController {
    @Resource
    ICollectionService collectionService;

    @Resource
    IUserService userService;

    @GetMapping("/add")
    public boolean addCollection(Principal principal,Integer commodityId){
        User user = userService.getByUsername(principal.getName());
        Integer userId = user.getId();
        Collection collection = new Collection()
                .setCommodityId(commodityId)
                .setUserId(userId);
        return collectionService.save(collection);
    }

    @GetMapping("/delete")
    public boolean deleteCollection(Principal principal,Integer commodityId){
        User user = userService.getByUsername(principal.getName());
        Integer userId = user.getId();
        QueryWrapper<Collection> query = new QueryWrapper<>();
        query.eq("commodity_id",commodityId);
        query.eq("user_id",user.getId());
        return collectionService.remove(query);
    }

    @GetMapping("/list")
    public List<CommodityDto> findlist(Principal principal){
        User user = userService.getByUsername(principal.getName());
        return collectionService.findList(user.getId());
    }

}
